import {
  Inject,
  Controller,
  Body,
  UseGuard,
  Post,
  Query,
  Get,
  Put,
} from '@midwayjs/core';
import { Context } from '@midwayjs/koa';
import { PatientService } from '../service/api.patient.service';
import { PatientDTO, UpdatePatientDTO } from '../dto/patient';
import { PageDTO, SearchDTO } from '../dto/page';
import { DoctorGuard } from '../guard/doctor.guard';
import { AuthGuard } from '../guard/auth.guard';
@Controller('/patient')
export class PatientController {
  @Inject()
  patientService: PatientService;
  @Inject()
  ctx: Context;
  /**
   * @name   createPatient 创建患者信息
   * @param  {PatientDTO}  patientDTO 创建患者信息DTO
   * @return string 创建患者信息是否成功
   */
  @UseGuard(DoctorGuard)
  @Post('/createPatient')
  async createPatient(@Body() patientDto: PatientDTO): Promise<string> {
    const departmentId = this.ctx.state.user.departmentId;
    const patient = await this.patientService.findPatient(
      departmentId,
      patientDto.name,
      patientDto.phone
    );
    if (patient) {
      throw new Error('患者已存在');
    }
    return await this.patientService.createPatient(patientDto);
  }
  /**
   * @name  getMyPatient 用户获取我的患者信息
   * @param  {PageDTO}  pageDto  分页DTO
   * @return {object} 我的患者信息
   */
  @UseGuard(DoctorGuard)
  @Get('/getMyPatient')
  async getMyPatient(@Query() pageDto: PageDTO): Promise<object> {
    if (pageDto.type == 2) {
      throw new Error('type参数错误');
    }
    const departmentId = this.ctx.state.user.departmentId;
    return await this.patientService.getMyPatient(departmentId, pageDto);
  }
  /**
   * @name  searchPatients 根据信息搜索患者
   * @param  {SearchDTO}  searchDto  搜索DTO
   * @param  {Object} 患者信息
   */
  @UseGuard(DoctorGuard)
  @Get('/searchPatients')
  async searchPatients(@Query() searchDto: SearchDTO): Promise<object> {
    const departmentId = this.ctx.state.user.departmentId;
    return await this.patientService.searchPatients(departmentId, searchDto);
  }
  /**
   * @name  getAllPatient 管理员获取所有患者信息
   * @param  {PageDTO}  pageDto  分页DTO
   * @return {object} 所有患者信息
   */
  @UseGuard(AuthGuard)
  @Get('/getAllPatient')
  async getAllPatient(@Query() pageDto: PageDTO): Promise<object> {
    if (pageDto.type == 2) {
      throw new Error('type参数错误');
    }
    return await this.patientService.getAllPatient(pageDto);
  }
  /**
   * @name  updatePatientInfo 修改患者信息
   * @param  {UpdatePatientDTO}  updatePatientDto  修改患者信息DTO
   * @return boolean 修改患者信息是否成功
   */
  @UseGuard(DoctorGuard)
  @Put('/updatePatientInfo')
  async updatePatientInfo(
    @Body() updatePatientDto: UpdatePatientDTO
  ): Promise<boolean> {
    const patient = await this.patientService.findPatientById(
      updatePatientDto.patientId
    );
    if (!patient) {
      throw new Error('患者不存在');
    }
    const filteredData = Object.entries(updatePatientDto).reduce(
      (acc, [key, value]) => {
        if (value != null) {
          // 同时过滤掉 null 和 undefined
          acc[key] = value;
        }
        return acc;
      },
      {} as UpdatePatientDTO
    );
    if (Object.keys(filteredData).length <= 2) {
      throw new Error('更新数据为空');
    }
    return await this.patientService.updatePatientInfo(filteredData);
  }
}
